package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.ScheduleRepository;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleRepository scheduleRepository;

    /**
     * 上传排班接口
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        //统一思路：先获取对象，判断有没有，然后再进行分类写
        //paramMap转换成Schedule对象
        String paramString = JSONObject.toJSONString(paramMap);
        Schedule schedule = JSONObject.parseObject(paramString, Schedule.class);
        //根据医院编号和排班编号进行查询
        Schedule scheduleExist = scheduleRepository.
                getScheduleByHoscodeAndHosScheduleId(schedule.getHoscode(),schedule.getHosScheduleId());
        //判断
        if(scheduleExist!=null) {  //如果里面有数据，那么进行更新
            scheduleExist.setUpdateTime(new Date());
            scheduleExist.setIsDeleted(0);
            schedule.setStatus(1);
            scheduleRepository.save(scheduleExist);
        }else{  //表明没有数据，进行上传  这里需要用岛createTime表示创建的时间
            schedule.setUpdateTime(new Date());
            schedule.setIsDeleted(0);
            schedule.setCreateTime(new Date());
            schedule.setStatus(1);
            scheduleRepository.save(schedule);
        }
    }

    /**
     * 查询排班接口
     * @param page
     * @param limit
     * @param scheduleQueryVo
     * @return
     */
    @Override
    public Page<Schedule> findPageSchedule(int page, int limit, ScheduleQueryVo scheduleQueryVo) {
        //创建pageable对象，设置当前页和每页记录数
        //0表示第一页
        PageRequest pageable = PageRequest.of(page - 1, limit);
        //创建Example对象
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleQueryVo,schedule);
        schedule.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher
                        .StringMatcher.CONTAINING)
                .withIgnoreCase(true);  //这里表示忽略大小写
        Example<Schedule> example = Example.of(schedule, matcher);
        Page<Schedule> all = scheduleRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除排班接口
     * @param hoscode
     * @param hosScheduleId
     */
    @Override
    public void remove(String hoscode, String hosScheduleId) {
        //根据医院编号 和 排班编号查询信息
        Schedule schedule = scheduleRepository.getScheduleByHoscodeAndHosScheduleId(hoscode, hosScheduleId);
        if(schedule != null) { //表示数据库中存在，则直接进行调用删除方法
            //调用方法进行删除
            scheduleRepository.deleteById(schedule.getId());
        }
    }
}
